package com.jiepos.mpos.core.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.dao.AppWorkflowTraceDao;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 工作流审核历史列表显示
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.core.tag 
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年9月27日 上午10:17:10
 * @history（历次修订内容、修订人、修订时间等）
 * @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
public class WorkFlowListTag extends BodyTagSupport {

	private static final long serialVersionUID = 7366326665143937358L;

	private final Logger log = LoggerFactory.getLogger(WorkFlowListTag.class);

	/**
	 * 业务类型
	 */
	private String system_type;
 
	/**
	 * 工作流业务主键
	 */
	private String system_key;
	/**
	 * 参数类型
	 */
	private String code_tp;
	@Override
	public int doEndTag() {
	 
		WebApplicationContext wac = WebApplicationContextUtils
				.getWebApplicationContext(pageContext.getServletContext());
		AppWorkflowTraceDao appWorkflowTraceDao = (AppWorkflowTraceDao) wac.getBean("appWorkflowTraceDao");  
		StringBuffer sb = new StringBuffer();
		List paramList = new ArrayList();
		sb.append("select WORKFLOW_UUID,LOGIN_NAME,SYSTEM_TYPE,SYSTEM_KEY,STATE_OPERATE,AUDIT_COMMENTS,REC_UPD_USR_ID,"
				+ "to_char(t1.REC_UPD_TS,'yyyy-mm-dd hh24:mi:ss') as REC_UPD_TS,to_char(t1.rec_crt_ts,'yyyy-mm-dd hh24:mi:ss') as REC_CRT_TS, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = ? ");
		sb.append(" and code_val = t1.old_state) as old_state, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = ? ");
		sb.append(" and code_val = t1.new_state) as new_state ");
		sb.append("from " + Table.T_APP_WORKFLOW_TRACE + " t1 ");
		sb.append("where 1=1 ");
		sb.append(" and t1.system_type = upper(?) ");
		sb.append(" and t1.system_key = ? order by t1.rec_id desc");
		paramList.add(code_tp);
		paramList.add(code_tp);
		paramList.add(system_type);
		paramList.add(system_key);
		List<Map<String, Object>> workFlowList = appWorkflowTraceDao.queryForList(sb.toString(), paramList);
		StringBuilder returnContent = new StringBuilder();
		returnContent.append("<div class=\"easyui-panel\" title=\"审核信息\">");
		returnContent.append("<table cellpadding=\"5\" style=\"width: 100%;border-collapse:collapse;\" border=\"1\">");
		returnContent.append("<tr><td>审核时间</td><td>审核人</td><td>审核状态</td><td>审核意见</td></tr>");
		Map workFlowMap = null;
		for(int i = 0 ; i < workFlowList.size() ; i++){
			workFlowMap = workFlowList.get(i);
			returnContent.append("<tr>");
			returnContent.append("<td>"+workFlowMap.get("rec_crt_ts")+"</td>");
			returnContent.append("<td>"+workFlowMap.get("login_name")+"</td>");
			returnContent.append("<td>"+workFlowMap.get("new_state")+"</td>");
			returnContent.append("<td>"+StringUtils.objToString(workFlowMap.get("audit_comments"))+"</td>");
			returnContent.append("</tr>");
		}
		returnContent.append("</table></div>");
		JspWriter writter = pageContext.getOut();
		try {
			writter.print(returnContent + "  ");
		} catch (IOException e) {
			log.error("buttonOperate: build error.");
		}
		return EVAL_PAGE;
	}
	public String getSystem_type() {
		return system_type;
	}
	public void setSystem_type(String system_type) {
		this.system_type = system_type;
	}
	public String getSystem_key() {
		return system_key;
	}
	public void setSystem_key(String system_key) {
		this.system_key = system_key;
	}
	public String getCode_tp() {
		return code_tp;
	}
	public void setCode_tp(String code_tp) {
		this.code_tp = code_tp;
	}
 

}
